﻿using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using SharpArch.Data.NHibernate;
using Magalie.Domaine.Depots;
using Magalie.Domaine;

namespace Magalie.Data
{
    public class GenDBTools : IGenDBTools
    {
        #region IGenDBTools Members

        /// <summary>
        /// Génération du shéma from scratch
        /// </summary>
        public void GenererLaBase()
        {
            Configuration cfg = NHibernateSession.LastConfiguration;
            if (cfg == null)
                return;

            SchemaExport schema = new SchemaExport(cfg);
            schema.Execute(false, true, false);

            InitData();
            NHibernateSession.Current.Flush();
        }

        /// <summary>
        /// Mise à jour du shéma
        /// </summary>
        public void MAJBase()
        {
            Configuration cfg = NHibernateSession.LastConfiguration;
            if (cfg == null)
                return;

            SchemaUpdate schema = new SchemaUpdate(cfg);
            schema.Execute(false, true);
        }

        #endregion

        private static void InitData()
        {
            Organisation mcnext = new Organisation(){ RaisonSociale="MCNext" };
            NHibernateSession.Current.Save(mcnext);

            Organisation kadeos = new Organisation() { RaisonSociale = "Kadeos" };
            NHibernateSession.Current.Save(kadeos);

            Projet mag = new Projet() { Organisation = mcnext, Nom = "Magalie", MappingUri="Magalie", Budget = 0 };
            NHibernateSession.Current.Save(mag);

            Projet kadeosfr = new Projet() { Organisation = kadeos, Nom = "KadeosFr", MappingUri="Kadeos", Budget = 0 };
            NHibernateSession.Current.Save(kadeosfr);

        }
    }
}
